using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Evolution.Engine.Managers
{
    public class TextEntry
    {
        private string _Font;
        private Vector2 _Position;
        private string _Text;
        private Color _FontColor;

        public Color FontColor { get { return _FontColor; } set { _FontColor = value; } }
        public string Text { get { return _Text; } set { _Text = value; } }
        public Vector2 Position { get { return _Position; } set { _Position = value; } }
        public string Font { get { return _Font; } set { _Font = value; } }

        public TextEntry(string Font, string Text, Vector2 Position, Color Color)
        {
            _Font = Font;
            _Text = Text;
            _Position = Position;
            _FontColor = Color;
        }
    }

    public class TextManager : Singleton<TextManager>
    {
        private List<TextEntry> _TextQueue = new List<TextEntry>();
        private SpriteBatch _SpriteBatch;

        public List<TextEntry> TextQueue { get { return _TextQueue; } set { _TextQueue = value; } }

        public TextManager()
        {
            _SpriteBatch = new SpriteBatch(GameEngine.Instance.Device);
        }

        public void Write(string Font, string Text, Vector2 position, Color color)
        {
            _TextQueue.Add(new TextEntry(Font, Text, position, color));
        }

        public void Draw(GameTime gameTime)
        {
            foreach (TextEntry text in _TextQueue)
            {
                _SpriteBatch.Begin(
                    SpriteBlendMode.AlphaBlend, 
                    SpriteSortMode.BackToFront, 
                    SaveStateMode.SaveState
                );
                
                _SpriteBatch.DrawString(
                    FontManager.Instance[text.Font],
                    text.Text,
                    text.Position,
                    text.FontColor
                );

                _SpriteBatch.End();
            }

            _TextQueue.Clear();
        }
    }
}
